package com.cx.controller;


import com.cx.pojo.Users;
import com.cx.service.LoginService;
import com.cx.service.impl.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/index")
public class LoginController {
    @Autowired
    private LoginService loginService;


    @GetMapping("/login")
    public String reg(Model model) {

        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String UserLogin(Users users, HttpServletRequest request, Model model, HttpSession session) {

        if ((loginService.checkUser(users.getUserName(), users.getUserPassword()) != null)) {
            Users loginUser = loginService.selectByName(users.getUserName());
            session.setAttribute("user", loginUser);
            return "success";
        } else if ((loginService.checkUser(users.getUserName(), users.getUserPassword()) == null)) {
            return "fail";
        }


        return "success";
    }


}
